package com.lx.core.spring;

import com.lx.constants.CommonConstants;
import com.lx.utils.IdWorkGetUtil;
import com.lx.utils.biz.BizIdWork;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;

/***
 * create by lanyxp on 2020/6/18 0018
 */
@Configuration
@Slf4j
public class CommonInitStart implements RunAfterStart {
    @Autowired
    private Environment env;
    @Value("${zk.address.registery}")
    private String zkServers;
    @Value("${spring.application.name}")
    private String appName;
    @Value("${com.lx.zk.prefix}")
    private String prefix;

    @Bean("bizIdWork")
    @Primary
    public BizIdWork SnowflakeIdWorker() {
        int dataCenter = 0;
        final long workId = IdWorkGetUtil.getSankInt(zkServers, dataCenter, appName);
        return new BizIdWork(workId, dataCenter, prefix);
    }
    @Override
    public void runInit(Object[] arges) {
        final String env = System.getProperty("env");
        if (!StringUtils.isBlank(env)) {
            System.setProperty(CommonConstants.PROJEC_LX_NAME, env);
        }
        if (!StringUtils.isBlank(env)) {
            System.setProperty(CommonConstants.SPRING_PROFILES_ACTIVE, env);
        }
    }
}
